package  com.net
{
 
 
	
	import com.game.chat.ChatCanvas;
	import com.game.login.ListActorBean;
	import com.game.login.ServerBean;
	import com.game.main.MainCanvas;
	import com.game.raceroom.RacingRoom;
	import com.game.raceroom.Room;
	import com.game.raceroom.loader.ItemLoader;
	import com.game.raceroom.player.CarInfo;
	import com.game.raceroom.player.UserBag;
	import com.game.raceroom.player.UserInfo;
	import com.game.raceroom.player.item.BaseItem;
	import com.game.raceroom.player.item.Car;
	import com.game.raceroom.player.item.Part;
	import com.game.roomlist.RoomListInfo;
	import com.util.HashMap;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.clearInterval;
	import flash.utils.getTimer;
	import flash.utils.setInterval;
	
	import mx.collections.ArrayCollection;
	import mx.utils.ArrayUtil;
	
	public class NetTrans
	{
		
		public static var ACTION_TYPE_SLOW:int = 0;
	    public static var ACTION_TYPE_FAST:int = 1;
	    //用户中心部分
		public static var C2S_ACTION_REG:int = 100;
	    public static var C2S_ACTION_LOGIN:int = 101;
		public static var S2C_REACTION_LOGIN:int = 101;
		
		//游戏世界部分
		//客户端发出
		public static var C2S_ACTION_GET_ACTORLIST:int = 200;
		public static var C2S_ACTION_CREATE_ACTOR:int = 201;
		public static var C2S_ACTION_SELECT_ACTOR:int=202;
		
		public static var C2S_ACTION_AVATAR_MOVE:int=302; 
		
		public static var C2S_ACTION_CHAT:int=400;
		public static var C2S_ACTION_CHANGE_MAP:int=304;
		public static var C2S_ACTION_USE_PROPERTY:int=503;
		public static var C2S_ACTION_DROP_PROPERTY:int=505;
		public static var C2S_ACTION_MOVE_PROPERTY:int=506;
		public static var C2S_ACTION_BUY_ITEM:int=507;
		public static var C2S_ACTION_SALE_ITEM:int=508;
		public static var C2S_ACTION_CLEANUP_BAG:int=509;
		public static var C2S_ACTION_ACCEPT_MISSION:int=602;
		public static var C2S_ACTION_DROP_MISSION:int=603;
		public static var C2S_ACTION_COMPLETE_MISSION:int=604;
		public static var C2S_ACTION_BUSINESS_REQUEST:int=800;
		public static var C2S_ACTION_BUSINESSREQUEST_ACCEPT:int=801;
		public static var C2S_ACTION_BUSINESSREQUEST_REFUSE:int=802;		
		public static var C2S_ACTION_ADD_BUSINESSITEM:int=804;
		public static var C2S_ACTION_REMOVE_BUSINESSITEM:int=805;
		public static var C2S_ACTION_REFRSH_BUSINESSMONEY:int=806;
        
		//服务器发出
		public static var S2C_REACTION_GET_ACTORLIST:int = 200;
		public static var S2C_REACTION_CREATE_ACTOR:int = 201;
		public static var S2C_REACTION_SELECT_ACTOR=202;
		
		public static var S2C_ACTION_OTHERAVATAR_ENTERGAME:int=300;
		public static var S2C_ACTION_OTHERAVATAR_LEAVEMAP:int=301;
		public static var S2C_ACTION_OTHERAVATAR_MOVE:int=303;		
		public static var S2C_ACTION_CHAT:int=400; 		
		public static var S2C_ACTION_REFRESH_BAG:int=500;		
		public static var S2C_REACTION_REFRESH_BAGITEM:int=504;
		public static var S2C_ACTION_REFRESH_CELLS:int=507;
		public static var S2C_REACTION_SALE_PROPERTY:int=508;
		public static var S2C_ACTION_REFRESH_EQUIP:int=509;
		public static var S2C_ACTION_NPC_MISSION:int=600;
		public static var S2C_ACTION_AVATAR_MISSION:int=601;
 
	
		public static var S2C_ACTION_ERROR:int=10000;
		public static var S2C_ACTION_TOPMSG:int=10002;
		public static var S2C_ACTION_SYSTEM_MSG:int=10003;
		public static var S2C_ACTION_WORLD_MSG:int=10004;
		public static var S2C_ACTION_REFRESH_ENVIROMENT_CODITION:int=700;
		public static var S2C_ACTION_BUSINESS_REQUEST:int=800;
		public static var S2C_ACTION_BUSINESS_START:int=803;
		public static var S2C_ACTION_Add_BUSINESSITEM:int=804; 
		public static var S2C_ACTOIN_REMOVE_BUSINESSITEM:int=805;
		public static var S2C_ACTION_REFRESH_BUSINESSMONEY:int=806;
		
		
		public static var C2S_ACTION_GET_ROOM_LIST = 2000;
		public static var C2S_ACTION_CREATE_ROOM = 2001;
		public static var C2S_ACTION_MATCH_ROOM = 2002;
		public static var C2S_ACTION_ENTER_ROOM = 2003;
		public static var C2S_ACTION_OUTOF_ROOM = 2004;
		public static var C2S_ACTION_OUTOF_ROOMLIST = 2005;
		public static var C2S_ACTION_REENTER_ROOM = 2006;
		public static var C2S_ACTION_KICKOUT_ROOM = 2007;
		public static var C2S_ACTION_READY = 2008;
		public static var C2S_ACTION_ABORT = 2009;
		public static var C2S_ACTION_CHANGE_SEAT = 2010;
		public static var C2S_ACTION_CHANGE_EQUIP = 2011;
		public static var C2S_ACTION_CHANGE_QUICK_KEY = 2012;
		
		
		public static var C2S_ACTION_RACE = 2100;
		
		
		
		
		
		
		
		
		
		public static var S2C_ACTION_GET_ROOM_LIST = 2000;
		public static var S2C_ACTION_GOTO_ROOM_LIST = 2001;
		public static var S2C_ACTION_REFRESH_ROOM_LIST = 2002;
		public static var S2C_ACTION_CREATE_ROOM_FAILD = 2003;
		public static var S2C_ACTION_ENTER_ROOM_FAILD = 2004;
		public static var S2C_ACTION_MATCH_ROOM_FAILD = 2005;
		public static var S2C_ACTION_CREATE_ROOM_OK = 2006;
		public static var S2C_ACTION_OTHERACTOR_ENTER_ROOM = 2007;
		public static var S2C_ACTION_ACTOR_ENTER_ROOM = 2008;
		public static var S2C_ACTION_ACTOR_OUTOF_ROOM = 2009;
		public static var S2C_ACTION_ACTOR_KICK_FROM_ROOM = 2010;
		public static var S2C_ACTION_ACTOR_CHANGE_SITE = 2011;
		public static var S2C_ACTION_ACTOR_READY = 2012;
		public static var S2C_ACTION_ACTOR_ABORT = 2013;
		public static var S2C_ACTION_LOCK_ROOM = 2014;
		public static var S2C_ACTION_GAME_START = 2015;
		public static var S2C_ACTION_STOP_JOIN_GAME = 2016;
		public static var S2C_ACTION_REFRESH_QUICK_SKILL = 2017;
		
		
		
		
		public static var S2C_ACTION_SEND_SCORE = 2100;
		public static var S2C_ACTION_RACE_OVER = 2101;
		public static var S2C_ACTION_INIT_RACE_ROOM = 2102;
		public static var S2C_ACTION_START_RACE = 2103;
		public static var S2C_ACTION_OTHER_START_RACE = 2104;
		public static var S2C_ACTION_OTHER_ACTION_ACTION = 2105;
		public static var S2C_ACTION_OTHER_MOVE_ACTION = 2106;
		public static var S2C_ACTION_FIX_POSITION = 2107;
		public static var S2C_ACTION_RACE_ACTION_HEADER = 2108;
		public static var S2C_ACTION_RETURN_RACING_ROOM = 2109;
		
	 
		
 
		
		private static var client:Client;
		public static var mainCanvas:MainCanvas;
		private static var chatCanvas:ChatCanvas;
		private static var main:Main;
		private static var dataArray:Array=new Array;
		private static var room:Room;
		private static var tid:int=-1;
		
		private static var msgRecord:Array=new Array();
		
		public function NetTrans()
		{
			
		}
		public static function setMainCanvas(g:MainCanvas):void{
			mainCanvas=g;
		}
		public static function setChatCanvas(g:ChatCanvas):void{
			chatCanvas=g;
		}
		public static function setRaceRoom(g:Room):void{
			room=g;
		}	
		public static function setGameEntry(g:Main):void{
			if(tid>=0)clearInterval(tid);
			tid=setInterval(praseData,10);
			main=g;
		}	       

		public static function setClient(c:Client){
			client=c;
		}
		public static function net_user_apply_login(uid:String,psd:String):void{
            var message:ByteArray=new ByteArray();
			 
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
            writeInt(message,NetTrans.C2S_ACTION_LOGIN);
            writeString(message,uid);
            writeString(message,psd);
			client.sendMessage(message);
		}
		public static function net_user_apply_reg(uid:String,psd:String):void{
			var message:ByteArray=new ByteArray();
			
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,NetTrans.C2S_ACTION_REG);
			writeString(message,uid);
			writeString(message,psd);
			client.sendMessage(message);
		}
		public static function net_get_actorlist(uid:int,uname:String):void{
			var message:ByteArray=new ByteArray();
			
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,NetTrans.C2S_ACTION_GET_ACTORLIST);
			writeInt(message,uid);
			writeString(message,uname);
			writeInt(message,Main.VERSION);
			client.sendMessage(message);
		}
		public static function net_create_actor(uid:int,uname:String,name:String,sex:int,job:int):void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,NetTrans.C2S_ACTION_CREATE_ACTOR);
			writeInt(message,uid);
			writeString(message,uname);
			writeString(message,name);
			writeInt(message,sex);
			writeInt(message,job);
			client.sendMessage(message);
		}
	
		
		public static function net_select_actor(actorId:int,uname:String):void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,NetTrans.C2S_ACTION_SELECT_ACTOR);
			writeInt(message,actorId);
			writeString(message,uname);
			client.sendMessage(message);
		}
	
		public static function net_chat(chanel:int,msg:String,faces:Array,uid:int):void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,NetTrans.C2S_ACTION_CHAT);
			writeByte(message,chanel);
			writeString(message,msg);
			writeByte(message,faces.length);
			if(faces.length>0){
				for(var i:int=0;i<faces.length;i++){
					trace(faces[i]);
					writeString(message,faces[i]);
				}
			}
			if(chanel == 2){
				writeInt(message,uid);
			}
			client.sendMessage(message);
 
			
			/* test enviroment condition */
//			var condition:int=int(msg);
//			if(condition is int)
//			{
//				if(condition >=0 && condition <= 3)
//				{
//					//mainCanvas.mapView.refreshEnviromentCondition(condition);
//				}
//			}
		}
		
		public static function net_get_room_list():void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_GET_ROOM_LIST);
			client.sendMessage(message);
		}
		public static function net_apply_create_room(roomName:String,roomPsd:String,mode:int,map:int,minlv:int,maxlv:int):void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_CREATE_ROOM);
			writeString(message,roomName);
			writeString(message,roomPsd);
			writeInt(message,mode);
			writeInt(message,map);
			writeInt(message,minlv);
			writeInt(message,maxlv);
			client.sendMessage(message);
		}	
		public static function net_apply_buy_items(items:Array):void{
			if(items.length==0)return;
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_BUY_ITEM);
			writeByte(message,items.length);
			for(var i:int=0;i<items.length;i++){
				var item:BaseItem=(BaseItem)(items[i]);
				writeInt(message,item.iid);
				writeShort(message,item.iqtt);
			}
			client.sendMessage(message);
		}
		public static function net_apply_sale_items(items:Array):void{
			if(items.length==0)return;
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_SALE_ITEM);
			writeByte(message,items.length);
			for(var i:int=0;i<items.length;i++){
				var item:BaseItem=(BaseItem)(items[i]);
			 
				writeInt(message,item.iid);
				writeShort(message,item.index);
				writeShort(message,item.iqtt);
			}
			client.sendMessage(message);
		}
		
		public static function net_apply_match_room():void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_MATCH_ROOM);
			client.sendMessage(message);
		}	
		public static function net_apply_enter_room(id:int):void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_ENTER_ROOM);
			writeInt(message,id);
			client.sendMessage(message);
		}
		public static function net_apply_kickout_room(id:int):void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_KICKOUT_ROOM);
			writeInt(message,id);
			client.sendMessage(message);
		}
		
		public static function net_apply_reenter_room():void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_REENTER_ROOM);
			client.sendMessage(message);
		}	
		
		public static function net_apply_outof_room():void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_OUTOF_ROOM);
			client.sendMessage(message);
		}	
		public static function net_apply_outof_roomlist():void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_OUTOF_ROOMLIST);
			client.sendMessage(message);
		}
		public static function net_apply_ready():void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_READY);
			client.sendMessage(message);
		}	
		public static function net_apply_abort():void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_ABORT);
			client.sendMessage(message);
		}	
		public static function net_change_seat(id:int):void{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_CHANGE_SEAT);
			writeInt(message,id);
			client.sendMessage(message);
		}	
		
		public static function net_move_property(originalIndex:int,propId:int,num:int,targetIndex:int):void
		{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_MOVE_PROPERTY);
			writeInt(message,originalIndex);
			writeInt(message,propId);
			writeInt(message,num);
			writeInt(message,targetIndex);
			client.sendMessage(message);
		}

		public static function net_cleanup_bag():void
		{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_CLEANUP_BAG);
			client.sendMessage(message);
		}
		public static function net_change_equip(list:Array):void
		{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_CHANGE_EQUIP);
			writeByte(message,list.length);
			while(list.length>0){
				writeShort(message,list.shift());
			}
			client.sendMessage(message);
		}
		public static function net_change_quick_key(list:Array):void
		{
			var message:ByteArray=new ByteArray();
			writeByte(message,NetTrans.ACTION_TYPE_SLOW);
			writeInt(message,C2S_ACTION_CHANGE_QUICK_KEY);
			for(var i:int=0;i<list.length;i++){ 
			 
				writeShort(message,list[i]);
			}
			client.sendMessage(message);
		}
		
		
		public static function setData(data:ByteArray){
			dataArray.push(data);
		}
		static var lastTime:Number = 0;
		public static function praseData():void{
			while(dataArray.length>0){
				var msg:ByteArray=dataArray.shift();
				
			    var bol:Boolean=true;
				var portocol:int=readInt(msg);
				while(bol){
					var time:Number = getTimer()-lastTime;
					trace("portocol "+portocol+"  "+time); 
					lastTime =  getTimer();
					bol=net_receive_package(portocol,msg)
					if(msg.bytesAvailable>0)portocol=readInt(msg);else break;
				}
				
			}
			
		}
		
        public static function net_receive_package(portocol:int,message:ByteArray):Boolean{
			

        	switch(portocol){
        		case S2C_REACTION_LOGIN:
	        		var text:String=readString(message);
	                var serverList:Array=new Array;
	                var uid:int;
					
	                	uid=readInt(message);
	                	var size:int=readInt(message);
		                for(var i:int=0;i<size;i++){
		                   var server:ServerBean=new ServerBean; 
		                   server.serverId=readShort(message);
		                   server.serverIp=readString(message);
		                   server.serverPort=readShort(message);
		                   server.serverName=readString(message);
		                   server.serverState=readByte(message);
		                   serverList.push(server);
		                }
					main.loginResult(1,uid,text,serverList);
	        		break;
				case S2C_REACTION_GET_ACTORLIST:
					var actorList:Array=new Array();
						
						var size:int=readByte(message);
						for(var i:int=0;i<size;i++){
							var actor:ListActorBean=new ListActorBean();
							actor.actorId=readInt(message);
							actor.nickname=readString(message);
							actor.sex=readByte(message);
							actor.level=readInt(message);
							actorList.push(actor); 
						}
			     	mainCanvas.showActorList(actorList);
					break;
				case S2C_REACTION_CREATE_ACTOR:
						var actor:ListActorBean=new ListActorBean();
						actor.nickname=readString(message);
						actor.actorId=readInt(message);
						actor.sex=readByte(message);
						actor.level=readInt(message);
					    mainCanvas.addNewListActor(actor); 
					break;
				case S2C_REACTION_SELECT_ACTOR:
		 			var userInfo:UserInfo = new UserInfo(0,"");
					userInfo.userID = readInt(message);
					userInfo.userNickName = readString(message);
					userInfo.sex = readByte(message);
					userInfo.level = readInt(message);
					userInfo.money = readInt(message);
					userInfo.iconId = readInt(message);
					MainCanvas.userInfo = userInfo;
					MainCanvas.userInfo.resetUi();
					break;
			 
				case S2C_ACTION_REFRESH_BAG:
					var bagSize:int=readShort(message);
					var itemSize:int=readShort(message);
				 
					var bag:UserBag =new UserBag(bagSize);
					var parts:Array = new Array;
					for(var i:int=0;i<itemSize;i++){
						 itemId=readInt(message);
						var itemNum:int=readShort(message);
						var itemIndex:int=readShort(message);
						var iuse:int=readByte(message);
						var itm:BaseItem=ItemLoader.getItem(itemId);
						itm.iqtt=itemNum;
						itm.iuse=iuse;
						itm.index=itemIndex;
						 trace(itemIndex+"   "+itm.iuse);
					    //bag.addItem(itm); 
						
						if(iuse==1){
							if(itm.itype==2){
								var car:Car=(Car)(ItemLoader.getCar(itemId));
								MainCanvas.userInfo.currentCar=new CarInfo(mainCanvas.uicanvas,car);
								car.iuse = 1;
								car.index=itemIndex;
								bag.addItem(car);
							}
							if(itm.itype==1){
								var part:Part=(Part)(ItemLoader.getPart(itemId));
								part.iuse = 1;
								part.index=itemIndex;
								bag.addItem(part);
								parts.push(part);
							}
								
						}else{
							bag.addItem(itm); 
						}
					}
			 
					
					if(MainCanvas.userInfo.currentCar){
						
						MainCanvas.userInfo.currentCar.car.parts =parts;
						MainCanvas.userInfo.currentCar.setPartInfo(MainCanvas.userInfo.currentCar.car);
						
					}
					mainCanvas.uicanvas.setCarInfo(MainCanvas.userInfo.currentCar);
					MainCanvas.userInfo.bag= bag;
					mainCanvas.UI.visible=true;
					if(mainCanvas.bagCanvas)mainCanvas.bagCanvas.clear();
					break;
				 	
				case S2C_ACTION_GET_ROOM_LIST:
					var roomList:HashMap=new HashMap();
					var size:int=readShort(message);
					for(var i:int=0;i<size;i++){
						var listInfo:RoomListInfo=new RoomListInfo;
						listInfo.roomId=readInt(message);
						listInfo.roomName=readString(message);
						listInfo.roomPsd=readString(message);
						listInfo.gameMode=readInt(message);
						listInfo.mapId=readInt(message);
						listInfo.userNo=readByte(message);
						listInfo.status=readByte(message);
						listInfo.minlv = readShort(message);
						listInfo.maxlv = readShort(message);
						roomList.put(listInfo.roomId,listInfo);
					}
					mainCanvas.refRoomList(roomList);
					break;
				case S2C_ACTION_REFRESH_ROOM_LIST:
					var roomList:HashMap = mainCanvas.roomList;
					
					var size:int=readShort(message);
					for(var i:int=0;i<size;i++){
						var roomid:int=readInt(message);
						if(roomid == -1) continue;
						var change:int=readByte(message);
						switch(change){
							case CHANGE_REMOVE_ROOM:
								if(!roomList)continue;
								if(!roomList.contensKey(roomid))continue;
								roomList.remove(roomid);
								break;
							case CHANGE_ROOM:
								
								var a:int=readByte(message);
								var b:int=readByte(message);
								var listInfo:RoomListInfo=(RoomListInfo)(roomList.getObjByKey(roomid));
								if(!roomList)continue;
								if(!roomList.contensKey(roomid))continue;
								listInfo.userNo=a;
								listInfo.status=b;
								break;
							case CHANGE_ADD_ROOM:
								listInfo=new RoomListInfo;
								listInfo.roomId=roomid;
								listInfo.roomName=readString(message);
								listInfo.roomPsd=readString(message);
								listInfo.gameMode=readInt(message);
								listInfo.mapId=readInt(message);
								listInfo.userNo=readByte(message);
								listInfo.status=readByte(message);
								listInfo.minlv = readShort(message);
								listInfo.maxlv = readShort(message);
								if(!roomList)continue;
								roomList.put(listInfo.roomId,listInfo);
								break;
						}
					}
					mainCanvas.refRoomList(roomList);
					break;
				case S2C_ACTION_GOTO_ROOM_LIST:
					mainCanvas.gotoRoomList();
					break;
				case S2C_ACTION_CREATE_ROOM_FAILD:
					mainCanvas.createRoomFaild();
					break;
				case S2C_ACTION_ENTER_ROOM_FAILD:
					mainCanvas.enterRoomFaild();
					break;
				case S2C_ACTION_MATCH_ROOM_FAILD:
					mainCanvas.matchRoomFaild();
					break;
				case S2C_ACTION_CREATE_ROOM_OK:
					var roomid:int=readInt(message);
					mainCanvas.createRoomOK(roomid);
					break;
				case S2C_ACTION_OTHERACTOR_ENTER_ROOM:
					var uid:int=readInt(message);
					var sid:int=readByte(message);
					var icon:String=readString(message);
					var name:String=readString(message);
					mainCanvas.otherEnterRoom(uid,sid,icon,name);
					break;
				case S2C_ACTION_ACTOR_ENTER_ROOM:
					var roomId:int= readInt(message);
					var name:String=readString(message);
					var map:int= readInt(message);
					var mode:int=readInt(message);
					var cuid:int=readInt(message);
					var size:int=readByte(message);
					var uids:Array= new Array;
					var sids:Array= new Array;
					var status:Array= new Array;
					var icons:Array= new Array;
					var names:Array= new Array;
				    for(var i:int=0;i<size ;i++){
						uids.push(readInt(message));
						sids.push(readByte(message));
						status.push(readByte(message));
						icons.push(readString(message));
						names.push(readString(message));
						 
					}
					mainCanvas.enterRoomOK(roomId,name,map,mode,uids,sids,status,icons,cuid,names);
					break;
				case S2C_ACTION_ACTOR_OUTOF_ROOM:
					var uid:int=readInt(message);
					var cuid:int=readInt(message);
					mainCanvas.userOutofRoom(uid,cuid);
					break;
				case S2C_ACTION_ACTOR_KICK_FROM_ROOM:
					var uid:int=readInt(message);
					var uname:String=readString(message);
					var cuname:String=readString(message);
					mainCanvas.kickOutResult(uid,uname,cuname);
					break;
				case S2C_ACTION_ACTOR_CHANGE_SITE:
					var uid:int=readInt(message);
					var site:int=readByte(message);
					mainCanvas.userChangeSeat(uid,site);
					break;
				case S2C_ACTION_ACTOR_READY:
					var uid:int=readInt(message);
					mainCanvas.userReady(uid);
					break;
				case S2C_ACTION_ACTOR_ABORT:
					var uid:int=readInt(message);
					mainCanvas.userAbort(uid);
					break;
				case S2C_ACTION_LOCK_ROOM:
					mainCanvas.lockRoom() ;
					break;
				case S2C_ACTION_GAME_START:
					var roomId:int=readInt(message);
					mainCanvas.startJoinGame(roomId);
					break;
				case  S2C_ACTION_RETURN_RACING_ROOM:
					var roomId:int=readInt(message);
					var mapId:int=readInt(message);
					var mode:int =readInt(message);
					mainCanvas.returnRacingRoom(roomId,mapId,mode);
					break;
				case S2C_ACTION_STOP_JOIN_GAME:
					mainCanvas.stopJoinGame();
					break;
				
				case S2C_ACTION_SEND_SCORE:
					room.receiveGameScore(message);
					break;
				
				case S2C_ACTION_RACE_OVER:
					room.destroy();
					break;
				case S2C_ACTION_INIT_RACE_ROOM:
					room.initPlayers(message);
					break;
				case S2C_ACTION_START_RACE:
					var time0=readInt(message);
					var time1=readInt(message);
					room.startGame(time0,time1);
					break;
				case S2C_ACTION_OTHER_START_RACE:
					var uid:int=readInt(message);
					room.otherStartGame(uid);
					break;
				case S2C_ACTION_OTHER_ACTION_ACTION:
					room.ac.doActionAction(message);
					break;
				case S2C_ACTION_OTHER_MOVE_ACTION:
					room.ac.doMoveAction(message);
					break;
				case S2C_ACTION_FIX_POSITION:
					room.ac.doFixPosition(message);
					break;
				case S2C_ACTION_RACE_ACTION_HEADER:
					room.ac.checkActions(message);
					break;
				case S2C_ACTION_REFRESH_CELLS:
				    size=readByte(message);
					for(var j:int = 0;j<size;j++){
				 
						var bItem:BaseItem = ItemLoader.getItem(readInt(message));
						
						bItem.iqtt = readShort(message);
						bItem.index = readShort(message);
						bItem.iuse = readByte(message);
						if(bItem.iqtt>0)
							MainCanvas.userInfo.bag.items.put(bItem.index,bItem);
						else
							MainCanvas.userInfo.bag.items.remove(bItem.index);
					}  
					MainCanvas.userInfo.money=readInt(message);
					mainCanvas.uicanvas.money.text = MainCanvas.userInfo.money+"";
					if(mainCanvas.shop&&mainCanvas.shop.buyCanvas)
						mainCanvas.shop.buyCanvas.buyResult();
					if(mainCanvas.shop&&mainCanvas.shop.sellCanvas)
						mainCanvas.shop.sellCanvas.sellResult();
					break;
				case S2C_REACTION_REFRESH_BAGITEM:
					var pos:int = readShort(message);
					var itemId:inte = readInt(message);
					var number:int = readShort(message);
					if(itemId == 0){
						MainCanvas.userInfo.bag.items.remove(pos);
						break;
					}
					var bItem:BaseItem = ItemLoader.getItem(itemId);
					
					bItem.iqtt = number;
					bItem.index =pos;
					bItem.iuse = 0;
					if(bItem.iqtt>0)
						MainCanvas.userInfo.bag.items.put(bItem.index,bItem);
					else
						MainCanvas.userInfo.bag.items.remove(bItem.index);
					break;
				case S2C_ACTION_REFRESH_EQUIP:
					size=readByte(message);
					var bag:UserBag= MainCanvas.userInfo.bag;
					for(var j:int = 0;j<bag.max_size;j++){
						 
						var bItem:BaseItem = (BaseItem)(bag.items.getObjByKey(j));
						if(bItem)
							bItem.iuse=0;
					}  
					for(var j:int = 0;j<size;j++){
						var id:int=readShort(message);
						var itm:BaseItem = (BaseItem)(bag.items.getObjByKey(id));
						itm.iuse=1;
					}  
					var parts:Array = new Array;
					for(var j:int = 0;j<bag.max_size;j++){
						
						var itm:BaseItem = (BaseItem)(bag.items.getObjByKey(j));
						if(itm==null)continue;
						if(itm.iuse==0)continue;
						 
						if(itm.itype==2){
							MainCanvas.userInfo.currentCar=new CarInfo(mainCanvas.uicanvas,(Car)(itm));
						}
						if(itm.itype==1){
							parts.push((Part)(itm));
						}
						 
					} 
					MainCanvas.userInfo.currentCar.car.parts =parts;
					MainCanvas.userInfo.currentCar.setPartInfo(MainCanvas.userInfo.currentCar.car);
				 
					mainCanvas.uicanvas.setCarInfo(MainCanvas.userInfo.currentCar);
					MainCanvas.userInfo.bag= bag;
					mainCanvas.UI.visible=true;
					if(mainCanvas.bagCanvas)mainCanvas.bagCanvas.clear();
					if(mainCanvas.bagCanvas)mainCanvas.bagCanvas.equipCanvas.result();
					break;
					
				case S2C_ACTION_REFRESH_QUICK_SKILL:
					var skills:Array= new Array;
					for(var i:int=0;i<8;i++){
						skills.push(readShort(message));
					}
					MainCanvas.userInfo.quickSkill = skills;
					break;
				case S2C_ACTION_ERROR:
					var msg:String=readString(message);
					Main.closePopWaiting();
					Main.popMsg(msg);
					break;
				case S2C_ACTION_CHAT:
					var chatObj:Object = new Object();
					chatObj.mode = readByte(message);
					chatObj.msgText = readString(message);
					chatObj.uid = readInt(message);
					chatObj.name = readString(message);
					var faces:Array = [];
					 
					var size:int = readByte(message);
					for(var i:int=0;i<size;i++){
						faces.push(readString(message));
					}
					chatObj.aFaces = faces;
					var talks = [];
					talks.push(chatObj);
					mainCanvas.chatcanvas.receiveMsg(talks);
					break;
				case S2C_ACTION_TOPMSG:
					var msg:String=readString(message);
					//Main.popTopMsg(msg);
					break;		
				case S2C_ACTION_SYSTEM_MSG:
					var msg:String=readString(message);
					//chatCanvas.receiveSystemMsg(msg);
					break;	
				case S2C_ACTION_WORLD_MSG:
					var msg:String=readString(message);
					mainCanvas.rollMsg.addRollMsg(msg);
					break;
				case 0:
					break;
				default:
					return false;
        	}
			
			return true; 
        }
		
		public static function saveToMsgRecord(messageTitle:String,messageInfo:String=null):void
		{
//			var msg:String=messageTitle;
//			if(messageInfo!=null) msg+=messageInfo;
//			msgRecord.push(msg);
//			MainCanvas.msgRecord.htmlText+="<p>"+msg+"</p>";
		}
		
		public static function readInt( message:ByteArray):int{
        	return message.readInt();
        }
		public static function readByte( message:ByteArray):int{
        	return message.readByte();
        }
		public static function readDouble( message:ByteArray):Number{
        	return message.readDouble();
        }
		public static function readLong( message:ByteArray):Number{
        	return message.readDouble();
        }
		public static function readShort( message:ByteArray):int{
        	return message.readShort();
        }
		public static function readString( message:ByteArray):String{
        	var len:int=message.readShort();
        	return message.readUTFBytes(len);
        }
		public  static function writeInt( message:ByteArray,i:int):void{
	    	message.writeInt(i);
	    }
		public  static function writeShort( message:ByteArray,i:int):void{
			message.writeShort(i);
		}
		public  static function writeByte( message:ByteArray,i:int):void{
	    	message.writeByte(i);
	    }
		public  static function writeDouble( message:ByteArray,i:Number):void{
	    	message.writeDouble(i);
	    }
		public  static function writeString( message:ByteArray,str:String):void{
	    	var bt:ByteArray=new ByteArray();
	    	bt.writeUTFBytes(str);
	    	message.writeShort(bt.length);
	    	message.writeBytes(bt);
	    }    
		public static function net_send_package(message:ByteArray){
			client.sendMessage(message);
		}
		public static var CHANGE_REMOVE_ROOM:int=0;
		public static var CHANGE_ADD_ROOM:int=1;
		public static var CHANGE_ROOM:int=2;   
	}

}